package com.fsy.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fsy.entities.Roles;
import com.fsy.entities.Users;
import com.fsy.service.RolemodulesService;
import com.fsy.service.RolesService;
import com.fsy.service.UserrolesService;
import com.fsy.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/roles")
public class RolesController {
    @Resource
    private RolesService rolesService;
    @Resource
    private RolemodulesService rolemodulesService;
    @Resource
    private UserrolesService userrolesService;

    @GetMapping("/selectRoles")
    public List<Roles> selectRoles(){
        return rolesService.list();
    }

    /**
     * 查询角色
     * @param roles
     * @return
     */
    @GetMapping("/selectRolesById")
    public Map<String,Object> selectRoles(Roles roles){
        //查询全部角色
        List<Roles> rolesList = rolesService.selectRolesById(roles);
        //查询全部角色数量
        int count = rolesService.selectCounts(roles);
        return Result.toClient("0", "",count,rolesList);
    }

    /**
     * 添加角色
     * @param roles
     * @return
     */
    @PostMapping("/insertRoles")
    public Map<String,Object> insertRoles(Roles roles){
        List<Roles> list = rolesService.list();
        for (int i=0;i<list.size();i++){
            if(roles.getName().equals(list.get(i).getName())){
                return Result.toClient("1", "已有此角色名称");
            }
        }
        Boolean rel = rolesService.save(roles);
        Map<String ,Object> rslmap =new HashMap<>();
        if (rel){
            rslmap.put("code","0");
            rslmap.put("msg","修改成功");
        }else{
            rslmap.put("code","1");
            rslmap.put("msg","修改失败");
        }
        return rslmap;
    }

    /**
     *删除角色
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Map<String ,Object> deleteById(@PathVariable Integer id){
        QueryWrapper wrapper=new QueryWrapper<>();
        wrapper.eq("roleId",id);
        Long count= rolemodulesService.count(wrapper);
        if (count>0){
            return Result.toClient("1","该角色下有模块，无法被删除");
        }
        Long count1= userrolesService.count(wrapper);
        if (count1>0){
            return Result.toClient("1","该角色下已被分配，无法删除");
        }
        boolean rel = rolesService.removeById(id);
        Map<String ,Object> rslmap =new HashMap<>();
        if (rel){
            rslmap.put("code","0");
            rslmap.put("msg","删除成功");
        }else{
            rslmap.put("code","1");
            rslmap.put("msg","删除失败");
        }
        return rslmap;
    }


    /**
     * 修改角色
     * @param roles
     * @return
     */
    @PostMapping("/updateRoles")
    public Map<String ,Object> updateRoles(Roles roles){
        List<Roles> list = rolesService.list();
        Roles r = rolesService.getById(roles.getId());
        if (!roles.getName().equals(r.getName())){
            for (int i=0;i<list.size();i++){
                if(roles.getName().equals(list.get(i).getName())){
                    return Result.toClient("0", "角色名称不能相同");
                }
            }
        }
        boolean rel = rolesService.updateById(roles);
        Map<String ,Object> rslmap =new HashMap<>();
        if (rel){
            rslmap.put("code","0");
            rslmap.put("msg","修改成功");
        }else{
            rslmap.put("code","1");
            rslmap.put("msg","修改失败");
        }
        return rslmap;
    }


}
